$(function(){
	var year = new Date().getFullYear();
	var month = new Date().getMonth();
	if(month<10)
		month="0"+month
	var date = (year+"-"+month);
	//初始化表格 考勤数据
	tableSJ(date);
	tableJG(date);
});

function tableJG(date){
	$('#KaoQinTongJidg').datagrid({
	    url:'KaoQinShuJuJieGuo',
	    queryParams:{
	    	date:date
	    },
	    columns:[[
			{field:'RESID',checkbox:true,title:'考勤数据编号',width:1,align:'center',hidden:true},
			{field:'CWASID',title:'所属考勤卡',width:1,align:'center'},
			{field:'EMPNAME',title:'员工姓名',width:1,align:'center'},
			{field:'DEPTNAME',title:'部门',width:1,align:'center'},
			{field:'TEMNAME',title:'班组',width:1,align:'center'},
			{field:'SHFNAME',title:'班次',width:1,align:'center'},
			{field:'SHFTIME',title:'班次时间',width:1,align:'center'},
			{field:'RESTIME',title:'上班总时长',width:1,align:'center'},
			{field:'QDCiShu',title:'签到次数',width:1,align:'center'},
			{field:'RESREM',title:'备注',width:1,align:'center'}
		]],
	    pagination:true,
	    pageNumber:1,
	    pageSize:5,
	    pageList:[5,10,15]
	});
}


function tableSJ(date){
	$('#KaoQinShuJudg').datagrid({
	    url:'KaoQinShuJu',
	    queryParams:{
	    	date:date
	    },
	    columns:[[
			{field:'RESID',checkbox:true,title:'考勤数据编号',width:1,align:'center',hidden:true},
			{field:'CWASID',title:'所属考勤卡',width:1,align:'center'},
			{field:'EMPNAME',title:'员工姓名',width:1,align:'center'},
			{field:'DEPTNAME',title:'部门',width:1,align:'center'},
			{field:'TEMNAME',title:'班组',width:1,align:'center'},
			{field:'SHFNAME',title:'班次',width:1,align:'center'},
			{field:'SHFTIME',title:'班次时间',width:2,align:'center'},
			{field:'RESMINTIME',title:'打卡签到',width:2,align:'center',
				formatter: function(value,row,index){
					return new Date(value).getFullYear() + '-' + (new Date(value).getMonth() + 1) + '-' + new Date(value).getDate() + ' ' + new Date(value).getHours() + ':' + new Date(value).getMinutes() + ':' + new Date(value).getSeconds();
				}
			},
			{field:'RESMAXTIME',title:'打卡签退',width:2,align:'center',
				formatter: function(value,row,index){
					return new Date(value).getFullYear() + '-' + (new Date(value).getMonth() + 1) + '-' + new Date(value).getDate() + ' ' + new Date(value).getHours() + ':' + new Date(value).getMinutes() + ':' + new Date(value).getSeconds();
				}
			},
			{field:'RESTIME',title:'上班时长',width:1,align:'center'},
			{field:'ZhuangTai',title:'考勤结果',width:2,align:'center',
				formatter: function(value,row,index){
					var SHFTIMEs = row["SHFTIME"].split("-")[0]+":00";
					var dates = new Date(row["RESMINTIME"]);
					var RESMINTIME = dates.getHours()+":"+dates.getMinutes()+":"+dates.getSeconds();
					
					var SHFTIMEx = row["SHFTIME"].split("-")[1]+":00";
					var datex = new Date(row["RESMAXTIME"]);
					var RESMAXTIME = datex.getHours()+":"+datex.getMinutes()+":"+datex.getSeconds();
					
					var s1 = dateBiJiao(SHFTIMEs,RESMINTIME);
					var s2 = dateBiJiao(SHFTIMEx,RESMAXTIME);
					var sum="";
					if(s1==0){
						sum += "(迟到)";
					}else {
						sum += "(正常)";
					}
					if(s2==1){
						sum += "(早退)";
					}else {
						sum += "(正常)";
					}
					return sum;
				}
			},
			{field:'RESREM',title:'备注',width:1,align:'center'}
		]],
	    pagination:true,
	    pageNumber:1,
	    pageSize:5,
	    pageList:[5,10,15]
	});
}

//两个时间判断先后
// 0 date1<date2  1 date1>date2   3 date1=date2
function dateBiJiao(date1,date2){
	var date3=date1.split(":");
	var date4=date2.split(":");
	if(parseInt(date3[0])>parseInt(date4[0])){
		return 1;
	}else if(parseInt(date3[0])==parseInt(date4[0])){
		if(parseInt(date3[1])>parseInt(date4[1])){
			return 1;
		}else if(parseInt(date3[1])==parseInt(date4[1])){
			if(parseInt(date3[2])>parseInt(date4[2])){
				return 1;
			}else if(parseInt(date3[2])==parseInt(date4[2])){
				return 3;
			}else if(parseInt(date3[2])<parseInt(date4[2])){
				return 0;
			}
		}else if(parseInt(date3[1])<parseInt(date4[1])){
			return 0;
		}
	}else if(parseInt(date3[0])<parseInt(date4[0])){
		return 0;
	}
}